package com.rzn.reading.service.impl;

import com.rzn.reading.BeanMapper;
import com.rzn.reading.BusinessException;
import com.rzn.reading.entity.Rank;
import com.rzn.reading.mapper.RankMapper;
import com.rzn.reading.model.dto.rank.CreateRankInputDTO;
import com.rzn.reading.service.RankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 评级表 服务实现类
 * </p>
 *
 * @author rzn
 * @since 2022-02-12
 */
@Service
public class RankServiceImpl implements RankService {
    @Autowired
    private RankMapper rankMapper;

    @Override
    public Boolean insertRank(CreateRankInputDTO createRankInputDTO) {
        Rank preRank = rankMapper.selectByUserIdAndLiteratureId(createRankInputDTO.getUserId(), createRankInputDTO.getLiteratureId());
        if (preRank!=null){
            throw new BusinessException("您已对该文献评级，不能重复评级！");
        }
        Rank rank = BeanMapper.map(createRankInputDTO, Rank.class);
        return rankMapper.insert(rank) == 1;
    }

    @Override
    public Double getAvgRank(Integer literatureId) {
        return rankMapper.selectByLiteratureId(literatureId);
    }
}
